package com.hrcx.mystery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrcx.mystery.common.CommonPage;
import com.hrcx.mystery.common.PageParamRequest;
import com.hrcx.mystery.dto.TasksSearchDto;
import com.hrcx.mystery.pojo.EbMysteryScheduleJob;
import com.hrcx.mystery.service.EbMysteryScheduleJobService;
import com.hrcx.mystery.mapper.EbMysteryScheduleJobMapper;
import com.hrcx.mystery.utils.quartz.MyTriggerListener;
import com.hrcx.mystery.utils.quartz.ScheduleUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author LinXiPeng
 */
@Slf4j
@Service
public class EbMysteryScheduleJobServiceImpl extends ServiceImpl<EbMysteryScheduleJobMapper, EbMysteryScheduleJob>
        implements EbMysteryScheduleJobService {
    @Resource
    private Scheduler scheduler;

    @Resource
    private EbMysteryScheduleJobMapper scheduleJobMapper;

    @Resource
    private MyTriggerListener myTriggerListener;

    @PostConstruct
    public void init() {
        List<EbMysteryScheduleJob> scheduleJobList = this.list();
        //注册全局监听器
        try {
            scheduler.getListenerManager().addSchedulerListener(myTriggerListener);
        } catch (SchedulerException e) {
            log.error("注册定时监听器时出现错误!");
        }
        for (EbMysteryScheduleJob scheduleJob : scheduleJobList) {
            CronTrigger trigger = ScheduleUtils.getTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if (trigger == null) {
                ScheduleUtils.createEbMysteryScheduleJob(scheduler, scheduleJob);
            } else {
                ScheduleUtils.updateEbMysteryScheduleJob(scheduler, scheduleJob);
            }
        }
    }

    @Override
    public Boolean runById(Long jid) {
        //立即执行任务
        return ScheduleUtils.run(scheduler, this.getById(jid));
    }

    @Override
    public Boolean resumeJob(Long jid) {
        if (ScheduleUtils.resumeJob(scheduler, jid)) {
            //恢复任务
            return this.lambdaUpdate().eq(EbMysteryScheduleJob::getJobId, jid).setSql("status=1").update();
        }
        return false;
    }

    @Override
    public Boolean pauseJob(Long jid) {
        if (ScheduleUtils.pauseJob(scheduler, jid)) {
            //停用任务
            return this.lambdaUpdate().eq(EbMysteryScheduleJob::getJobId, jid).setSql("status=0").update();
        }
        return false;
    }

    @Override
    public Boolean deleteJob(Long jid) {
        if (ScheduleUtils.deleteJob(scheduler, jid)) {
            //删除任务
            return this.removeById(jid);
        }
        return false;
    }

    @Override
    public Boolean createJob(EbMysteryScheduleJob scheduleJob) {
        //添加前先更新一下自增值
        scheduleJobMapper.resetIncrement();
        if (this.save(scheduleJob)) {
            //如果数据库添加成功就在quartz中创建一下任务
            ScheduleUtils.createEbMysteryScheduleJob(scheduler, scheduleJob);
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateJob(EbMysteryScheduleJob scheduleJob) {
        if (this.saveOrUpdate(scheduleJob)) {
            //更新任务
            return ScheduleUtils.updateEbMysteryScheduleJob(scheduler, scheduleJob);
        }
        return false;
    }

    @Override
    public PageInfo<EbMysteryScheduleJob> getTaskList(TasksSearchDto tasksSearchDto, PageParamRequest pageParamRequest) {
        Page<EbMysteryScheduleJob> scheduleJobPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        //创建条件包装类
        LambdaQueryWrapper<EbMysteryScheduleJob> jobLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (tasksSearchDto.getStatus() != null) {
            jobLambdaQueryWrapper.eq(EbMysteryScheduleJob::getStatus, tasksSearchDto.getStatus());
        }
        List<EbMysteryScheduleJob> list = this.list(jobLambdaQueryWrapper);
        return CommonPage.copyPageInfo(scheduleJobPage, list);
    }
}